//
//  XLDoctor
//
//  Created by longma on 2018/7/31.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "WCBDHelper.h"
#import "MJGoutDetectionModel+WCTTableCoding.h"
#import "RecordDataBaseModel+WCTTableCoding.h"
#import "MJMultipleModel+WCTTableCoding.h"
#import "PatientModel+WCTTableCoding.h"
#import <WCDBObjc/WCDBObjc.h>

#define kPatientTable @"PatientTable"
#define kGoutTable @"GoutTable"
#define kMultipleTable @"MultipleTable"
#define kHemoglobinTable @"HemoglobinTable"
#define kBloodSugarTable @"BloodSugarTable"
#define kTotalCholesterolTable @"TotalCholesterolTable"



@interface WCBDHelper ()

@property (strong,nonatomic) WCTDatabase *dataBase;

@end

@implementation WCBDHelper


/**
 单例
 */
+ (instancetype)shared{
    static WCBDHelper *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[WCBDHelper alloc] init];
    });
    return instance;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        [self createDataBase];
    }
    return self;
}

-(void)createDataBase{
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dbPath = [documentPath stringByAppendingString:@"health.db"];
    
    WCTDatabase* database = [[WCTDatabase alloc] initWithPath:dbPath];
    self.dataBase = database;
    if ([self.dataBase canOpen]) {
        NSLog(@"创建数据库成功");
        
    }else{
        NSLog(@"创建数据库失败");
    }
}

- (void)removeFiles {
    
    BOOL result= [WCBDHelper.shared.dataBase removeFiles]; //删除所有文件表
    if(result){
        NSLog(@"表删除成功=======================");
    }
}

- (void)initHealthData{
//    self.isNeedUpdateHealthData = true;
    if(self.isNeedUpdateHealthData){
        self.isNeedUpdatePatientData = true;

        self.isNeedUpdateGoutData = true;
        self.isNeedUpdateMultipleData = true;
        self.isNeedUpdateHemoglobinData = true;
        self.isBloodSugarUpdated = true;
        self.isTotalCholesterolUpdated = true;
        
        if(self.isNeedUpdatePatientData){
            [self requestPatientListData];
        }
        if(self.isNeedUpdateGoutData){
            [self requestGoutListDataWithLoading:false dataArrayBlock:nil];
        }
        if(self.isNeedUpdateHemoglobinData){
            [self requestHemoglobinListDataWithLoading:false dataArrayBlock:nil];
        }
        if(self.isNeedUpdateMultipleData){
            [self requestMultipleListDataWithLoading:false dataArrayBlock:nil];
        }
        if(self.isBloodSugarUpdated){
            [self requestBloodSugarListDataWithLoading:false dataArrayBlock:nil];
        }
        if(self.isTotalCholesterolUpdated){
            [self requestTotalCholesterolListDataWithLoading:false dataArrayBlock:nil];
        }
        
        self.isNeedUpdateHealthData = false;
    }
    
    
}

#pragma mark - 成员
/// 增加一条数据
+ (PatientModel *)mj_insertObjectWithPatient:(PatientModel *)patientModel{
    
    NSString *userId = UserManager.shared.userModel.userId;
    
    PatientModel* object = [WCBDHelper.shared.dataBase getObjectOfClass:PatientModel.class fromTable:kPatientTable where:PatientModel.userId == userId && PatientModel.patientId == patientModel.patientId];
    if(object){
        BOOL ret = [WCBDHelper.shared.dataBase deleteFromTable:kPatientTable where:PatientModel.userId == userId && PatientModel.patientId == patientModel.patientId];
        if(ret){
            
            BOOL success = [WCBDHelper.shared.dataBase insertObject:patientModel intoTable:kPatientTable];
            if(success){
                return patientModel;
            }
        }
    }else{
        BOOL success = [WCBDHelper.shared.dataBase insertObject:patientModel intoTable:kPatientTable];
        if(success){
            return patientModel;
        }
    }
    
    return [PatientModel new];
}

- (void)mj_initPatientArray{
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSMutableArray *resultArr = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSArray<PatientModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:PatientModel.class fromTable:kPatientTable where:PatientModel.userId == userId orders:PatientModel.createDateTime.asOrder(WCTOrderedDescending)];//倒序查询
    
//    NSArray *duplicatedArr = [NSArray yy_modelArrayWithClass:[PatientModel class] json:[allObjects yy_modelToJSONData]];
    [resultArr addObjectsFromArray:allObjects];
    
    HealthHelper.shared.homePatientList = resultArr;
}

+ (NSMutableArray *)mj_getPatientArrayWithSearchString:(NSString *)srarchString {
    
    NSString *matchString = [[@"%" stringByAppendingString:srarchString] stringByAppendingString:@"%"];;
    NSString *userId = UserManager.shared.userModel.userId;
    NSMutableArray *resultArr = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSArray<PatientModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:PatientModel.class fromTable:kPatientTable where:PatientModel.userId == userId && (PatientModel.patientName.like(matchString) || PatientModel.phoneNumber.like(matchString))];
    
//    NSArray *duplicatedArr = [NSArray yy_modelArrayWithClass:[PatientModel class] json:[allObjects yy_modelToJSONData]];
    [resultArr addObjectsFromArray:allObjects];
    return resultArr;
}

- (void)requestPatientListData{
    
    if(![self.dataBase tableExists:kPatientTable]){
        BOOL result = [self.dataBase createTable:kPatientTable withClass:PatientModel.class];
        if (!result) {
            NSLog(@"创建受检者表失败");
        }else{
            NSLog(@"创建受检者表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:patient_select requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isNeedUpdatePatientData = false;
            NSArray *data = model.data;
            NSMutableArray *modelArray = [NSMutableArray array];
            for (int i = 0; i<data.count; i++) {
                PatientModel *patientModel = [PatientModel yy_modelWithDictionary:data[i]];
                PatientModel *rmodel = [WCBDHelper mj_insertObjectWithPatient:patientModel];
                [modelArray addObject:rmodel];
            }
            HealthHelper.shared.homePatientList = modelArray;
         }else if (model.code ==  401){
            
         }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - Gout
- (NSArray *)mj_getGoutArrayWithParm:(MJCommonModel *)parm {
    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJGoutDetectionModel.userId == userId && MJGoutDetectionModel.noUpload == false);

    WCDB::Expression condition = WCDB::Expression(userIdExpression);
    if(parm.patientId){
        
        WCDB::Expression patientIdExpression = WCDB::Expression(MJGoutDetectionModel.patientId == parm.patientId);
        condition =  WCDB::Expression(userIdExpression && patientIdExpression);
    }
    if(parm.minDate && parm.maxDate){
//        NSString *likeMinDate = [[@"%" stringByAppendingString:parm.minDate] stringByAppendingString:@"%"];
//        WCDB::Expression minDateExpression = WCDB::Expression(MJGoutDetectionModel.createDateTime.like(likeMinDate));
//        
//        NSString *likeMaxDate = [[@"%" stringByAppendingString:parm.maxDate] stringByAppendingString:@"%"];
//        WCDB::Expression maxDateExpression = WCDB::Expression(MJGoutDetectionModel.createDateTime.like(likeMaxDate));
        
        WCDB::Expression dateExpression = WCDB::Expression(MJGoutDetectionModel.createDateTime > parm.minDate && MJGoutDetectionModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId){
        
        WCDB::Expression patientIdExpression = WCDB::Expression(MJGoutDetectionModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJGoutDetectionModel.createDateTime > parm.minDate && MJGoutDetectionModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression);
    }
    
//    NSArray<MJGoutDetectionModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJGoutDetectionModel.class fromTable:kGoutTable where:condition];
    
    NSArray<MJGoutDetectionModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJGoutDetectionModel.class fromTable:kGoutTable where:condition orders:MJGoutDetectionModel.createDateTime.asOrder(WCTOrderedDescending)];
    return allObjects;
}

- (void)requestGoutListDataWithLoading:(BOOL)showLoading dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:GoutDetection_select requestType:RequestTypeGet showLoading:showLoading success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isNeedUpdateGoutData = false;
            NSArray *data = model.data;
            [WCBDHelper mj_insertGoutObjectsWithArray:data dataArrayBlock:dataArrayBlock];
         }
    } failure:^(NSError *error) {
        
    }];
}

+ (void)mj_insertGoutObjectsWithArray:(NSArray *)array dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    if(![WCBDHelper.shared.dataBase tableExists:kGoutTable]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:kGoutTable withClass:MJGoutDetectionModel.class];
        if (!result) {
            NSLog(@"创建尿酸表失败");
        }else{
            NSLog(@"创建尿酸表成功");
        }
    }else{
        NSLog(@"存在尿酸表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSArray *arrayItems =  [NSArray yy_modelArrayWithClass:[MJGoutDetectionModel class] json:array];
    NSArray* modelArray = [[arrayItems reverseObjectEnumerator] allObjects];

    NSArray<MJGoutDetectionModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJGoutDetectionModel.class fromTable:kGoutTable where:MJGoutDetectionModel.userId == userId];
    
    if(allObjects.count){
        BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:kGoutTable where:MJGoutDetectionModel.userId == userId];
        if(success){
            BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kGoutTable];
            if(result){
                NSLog(@"MJLog=======================插入成功");
            }
        }
    }else{
        BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kGoutTable];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
    if(dataArrayBlock)
     dataArrayBlock(modelArray);
    
}

- (void)mj_insertGoutObjectWithModel:(MJGoutDetectionModel *)model{
    
    
    MJGoutDetectionModel *itemModel = [WCBDHelper.shared.dataBase getObjectOfClass:MJGoutDetectionModel.class fromTable:kGoutTable where:MJGoutDetectionModel.detectionId == model.detectionId];
    
    if(itemModel){
        BOOL success = [self.dataBase deleteFromTable:kGoutTable where:MJGoutDetectionModel.detectionId == model.detectionId];
         if(success){
             NSLog(@"MJLog=======================尿酸删除成功");
         }
    }
    
    BOOL result = [self.dataBase insertObject:model intoTable:kGoutTable];
    if(result){
        NSLog(@"MJLog=======================插入尿酸成功");
    }else{
        NSLog(@"MJLog=======================插入尿酸失败");
    }
}

- (void)mj_uploadLocalGoutArray {
    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJGoutDetectionModel.userId == userId && MJGoutDetectionModel.noUpload == true);
    NSArray<MJGoutDetectionModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJGoutDetectionModel.class fromTable:kGoutTable where:userIdExpression orders:MJGoutDetectionModel.createDateTime.asOrder(WCTOrderedDescending)];
    for (MJGoutDetectionModel *item in allObjects) {
        
        NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{
            @"period":item.period,
            @"patientId":item.patientId,
        }];
        [parms setObject:item.bloodUnit forKey:@"bloodUnit"];
        [parms setObject:@(item.bloodUricAcid).description forKey:@"bloodUricAcid"];
        [parms setObject:item.createDateTime forKey:@"createDateTime"];
        [self uploadLocalDetectionWithParms:parms detectionItemType:DetectionItemTypeGout detectionId:item.detectionId];
    }
}

- (void)uploadLocalDetectionWithParms:(NSMutableDictionary *)parms detectionItemType:(DetectionItemType)detectionItemType detectionId:(NSString *)detectionId{
    NSString *userId = UserManager.shared.userModel.userId;

    NSString *url;
    if(detectionItemType == DetectionItemTypeGout){
        url = GoutDetection_select;
    }
    
    [MJHttpNetWorking requestWithParams:parms requestUri:url requestType:RequestTypePost showLoading:false success:^(MJBaseModel *model) {
        
        if(model.code == 200){
            if(detectionItemType == DetectionItemTypeGout){
                MJGoutDetectionModel *goutDetectionModel = [MJGoutDetectionModel yy_modelWithDictionary:model.data];
                BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:kGoutTable where:MJGoutDetectionModel.detectionId == detectionId && MJGoutDetectionModel.userId == userId];
                if(success){
                    [self mj_insertGoutObjectWithModel:goutDetectionModel];
                }
            }
        }

    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - Hemoglobin
- (void)requestHemoglobinListDataWithLoading:(BOOL)showLoading dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:HemoglobinDetection_select requestType:RequestTypeGet showLoading:showLoading success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isNeedUpdateHemoglobinData = false;
            NSArray *data = model.data;
            [WCBDHelper mj_insertHemoglobinObjectsWithArray:data dataArrayBlock:dataArrayBlock];
         }
    } failure:^(NSError *error) {
        
    }];
}
- (void)mj_insertHemoglobinObjectWithModel:(MJHemoglobinModel *)model{
    
    MJHemoglobinModel *itemModel = [WCBDHelper.shared.dataBase getObjectOfClass:MJHemoglobinModel.class fromTable:kHemoglobinTable where:MJHemoglobinModel.detectionId == model.detectionId];
    
    if(itemModel){
        BOOL success = [self.dataBase deleteFromTable:kHemoglobinTable where:MJHemoglobinModel.detectionId == model.detectionId];
         if(success){
             NSLog(@"MJLog=======================血红蛋白删除成功");
         }
    }
    
    BOOL result = [self.dataBase insertObject:model intoTable:kHemoglobinTable];
    if(result){
        NSLog(@"MJLog=======================血红蛋白插入成功");
    }
}
+ (void)mj_insertHemoglobinObjectsWithArray:(NSArray *)array dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    if(![WCBDHelper.shared.dataBase tableExists:kHemoglobinTable]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:kHemoglobinTable withClass:MJHemoglobinModel.class];
        if (!result) {
            NSLog(@"创建受检者表失败");
        }else{
            NSLog(@"创建受检者表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSArray *arrayItems =  [NSArray yy_modelArrayWithClass:[MJHemoglobinModel class] json:array];
    NSArray* modelArray = [[arrayItems reverseObjectEnumerator] allObjects];

    NSArray<MJHemoglobinModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJHemoglobinModel.class fromTable:kHemoglobinTable where:MJHemoglobinModel.userId == userId];
    
    if(allObjects.count){
        BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:kHemoglobinTable where:MJHemoglobinModel.userId == userId];
        if(success){
            BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kHemoglobinTable];
            if(result){
                NSLog(@"MJLog=======================插入成功");
            }
        }
    }else{
        BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kHemoglobinTable];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
    if(dataArrayBlock)
     dataArrayBlock(modelArray);
    
}

- (NSArray *)mj_getHemoglobinArrayWithParm:(MJCommonModel *)parm {

    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJHemoglobinModel.userId == userId);

    WCDB::Expression condition = WCDB::Expression(userIdExpression);
    if(parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJHemoglobinModel.patientId == parm.patientId);
        condition =  WCDB::Expression(userIdExpression && patientIdExpression);
    }
    if(parm.minDate && parm.maxDate){
        WCDB::Expression dateExpression = WCDB::Expression(MJHemoglobinModel.createDateTime > parm.minDate && MJHemoglobinModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJHemoglobinModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJHemoglobinModel.createDateTime > parm.minDate && MJHemoglobinModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression);
    }
    
    NSArray<MJHemoglobinModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJHemoglobinModel.class fromTable:kHemoglobinTable where:condition orders:MJHemoglobinModel.createDateTime.asOrder(WCTOrderedDescending)];
    return allObjects;
}

#pragma mark - Multiple
- (void)requestMultipleListDataWithLoading:(BOOL)showLoading dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    [MJHttpNetWorking requestWithParams:@{@"isMultiple":@1} requestUri:MultipleDetection_select requestType:RequestTypeGet showLoading:showLoading success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isNeedUpdateMultipleData = false;
            NSArray *data = model.data;
            [WCBDHelper mj_insertMultipleObjectsWithArray:data dataArrayBlock:dataArrayBlock];
         }
    } failure:^(NSError *error) {
        
    }];
}

+ (void)mj_insertMultipleObjectsWithArray:(NSArray *)array dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    if(![WCBDHelper.shared.dataBase tableExists:kMultipleTable]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:kMultipleTable withClass:MJMultipleModel.class];
        if (!result) {
            NSLog(@"创建4合1表失败");
        }else{
            NSLog(@"创建4合1表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSArray *arrayItems =  [NSArray yy_modelArrayWithClass:[MJMultipleModel class] json:array];
    NSArray* modelArray = [[arrayItems reverseObjectEnumerator] allObjects];

    NSArray<MJMultipleModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJMultipleModel.class fromTable:kMultipleTable where:MJMultipleModel.userId == userId];
    
    if(allObjects.count){
        BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:kMultipleTable where:MJMultipleModel.userId == userId];
        if(success){
            BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kMultipleTable];
            if(result){
                NSLog(@"MJLog=======================插入成功");
            }
        }
    }else{
        BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:kMultipleTable];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
    if(dataArrayBlock)
     dataArrayBlock(modelArray);
    
}

- (NSArray *)mj_getMultipleArrayWithParm:(MJCommonModel *)parm {

    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJMultipleModel.userId == userId);

    WCDB::Expression condition = WCDB::Expression(userIdExpression);
    if(parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJMultipleModel.patientId == parm.patientId);
        condition =  WCDB::Expression(userIdExpression && patientIdExpression);
    }
    if(parm.minDate && parm.maxDate){
        WCDB::Expression dateExpression = WCDB::Expression(MJMultipleModel.createDateTime > parm.minDate && MJMultipleModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJMultipleModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJMultipleModel.createDateTime > parm.minDate && MJMultipleModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression);
    }
    
    NSArray<MJMultipleModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJMultipleModel.class fromTable:kMultipleTable where:condition orders:MJMultipleModel.createDateTime.asOrder(WCTOrderedDescending)];
    return allObjects;
}

/// 插入4合1数据
/// - Parameter model: <#model description#>
- (void)mj_insertMultipleObjectWithModel:(MJMultipleModel *)model {
    
    if(![WCBDHelper.shared.dataBase tableExists:kMultipleTable]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:kMultipleTable withClass:MJMultipleModel.class];
        if (!result) {
            NSLog(@"创建受检者表失败");
        }else{
            NSLog(@"创建受检者表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    MJMultipleModel* object = [self.dataBase getObjectOfClass:MJMultipleModel.class fromTable:kMultipleTable where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == model.detectionId];
    
    if(object){
        BOOL result = false;
        if(model.goutDetection){
            result = [self.dataBase updateTable:kMultipleTable setProperty:MJMultipleModel.goutDetection toValue:model.goutDetection where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == model.detectionId];
        }
        if(model.hemoglobin){
            result = [self.dataBase updateTable:kMultipleTable setProperty:MJMultipleModel.hemoglobin toValue:model.hemoglobin where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == model.detectionId];
        }
        if(model.bloodGlucose){
            result = [self.dataBase updateTable:kMultipleTable setProperty:MJMultipleModel.bloodGlucose toValue:model.bloodGlucose where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == model.detectionId];
        }
        if(model.totalCholesterol){
            result = [self.dataBase updateTable:kMultipleTable setProperty:MJMultipleModel.totalCholesterol toValue:model.totalCholesterol where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == model.detectionId];
        }
        if(result){
            NSLog(@"MJLog=======================更新成功");
        }
    }else{
        BOOL result = [self.dataBase insertObject:model intoTable:kMultipleTable];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
}

- (MJMultipleModel *)mj_getMultipleObjectWithDetectionId:(NSString *)detectionId{
    NSString *userId = UserManager.shared.userModel.userId;
    MJMultipleModel* object = [self.dataBase getObjectOfClass:MJMultipleModel.class fromTable:kMultipleTable where:MJMultipleModel.userId == userId && MJMultipleModel.detectionId == detectionId];
    
    return object;
}


#pragma mark - 血糖
- (void)requestBloodSugarListDataWithLoading:(BOOL)showLoading dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:BloodGlucoseDetection_select requestType:RequestTypeGet showLoading:showLoading success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isBloodSugarUpdated = false;
            NSArray *data = model.data;
            [WCBDHelper mj_insertBloodSugarObjectsWithArray:data dataArrayBlock:dataArrayBlock];
         }
    } failure:^(NSError *error) {
        
    }];
}
- (void)mj_insertBloodSugarObjectWithModel:(MJBloodSugarModel *)model{
    
    MJBloodSugarModel *itemModel = [WCBDHelper.shared.dataBase getObjectOfClass:MJBloodSugarModel.class fromTable:kBloodSugarTable where:MJBloodSugarModel.detectionId == model.detectionId];
    
    if(itemModel){
        BOOL success = [self.dataBase deleteFromTable:kBloodSugarTable where:MJBloodSugarModel.detectionId == model.detectionId];
         if(success){
             NSLog(@"MJLog=======================血糖删除成功");
         }
    }
    
    BOOL result = [self.dataBase insertObject:model intoTable:kBloodSugarTable];
    if(result){
        NSLog(@"MJLog=======================血糖插入成功");
    }
}
+ (void)mj_insertBloodSugarObjectsWithArray:(NSArray *)array dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    NSString *tableName = kBloodSugarTable;
    
    if(![WCBDHelper.shared.dataBase tableExists:tableName]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:tableName withClass:MJBloodSugarModel.class];
        if (!result) {
            NSLog(@"创建受检者表失败");
        }else{
            NSLog(@"创建受检者表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSArray *arrayItems =  [NSArray yy_modelArrayWithClass:[MJBloodSugarModel class] json:array];
    NSArray* modelArray = [[arrayItems reverseObjectEnumerator] allObjects];

    NSArray<MJBloodSugarModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJBloodSugarModel.class fromTable:tableName where:MJBloodSugarModel.userId == userId];
    
    if(allObjects.count){
        BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:tableName where:MJBloodSugarModel.userId == userId];
        if(success){
            BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:tableName];
            if(result){
                NSLog(@"MJLog=======================插入成功");
            }
        }
    }else{
        BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:tableName];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
    if(dataArrayBlock)
     dataArrayBlock(modelArray);
    
}

- (NSArray *)mj_getBloodSugarArrayWithParm:(MJCommonModel *)parm {

    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJBloodSugarModel.userId == userId);

    WCDB::Expression condition = WCDB::Expression(userIdExpression);
    if(parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJBloodSugarModel.patientId == parm.patientId);
        condition =  WCDB::Expression(userIdExpression && patientIdExpression);
    }
    if(parm.minDate && parm.maxDate){
        WCDB::Expression dateExpression = WCDB::Expression(MJBloodSugarModel.createDateTime > parm.minDate && MJBloodSugarModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJBloodSugarModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJBloodSugarModel.createDateTime > parm.minDate && MJBloodSugarModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression);
    }
    if(parm.patientId && parm.period){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJBloodSugarModel.patientId == parm.patientId);
        WCDB::Expression periodExpression = WCDB::Expression(MJBloodSugarModel.period == parm.period);
        condition = WCDB::Expression(userIdExpression  && patientIdExpression && periodExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId && parm.period){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJBloodSugarModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJBloodSugarModel.createDateTime > parm.minDate && MJBloodSugarModel.createDateTime < parm.maxDate);
        WCDB::Expression periodExpression = WCDB::Expression(MJBloodSugarModel.period == parm.period);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression && periodExpression);
    }

    NSArray<MJBloodSugarModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJBloodSugarModel.class fromTable:kBloodSugarTable where:condition orders:MJBloodSugarModel.createDateTime.asOrder(WCTOrderedDescending)];
    return allObjects;
}


#pragma mark - 总胆固醇
- (void)requestTotalCholesterolListDataWithLoading:(BOOL)showLoading dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:TotalCholesterolDetection_select requestType:RequestTypeGet showLoading:showLoading success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            WCBDHelper.shared.isTotalCholesterolUpdated = false;
            NSArray *data = model.data;
            [WCBDHelper mj_insertTotalCholesterolObjectsWithArray:data dataArrayBlock:dataArrayBlock];
         }
    } failure:^(NSError *error) {
        
    }];
}
- (void)mj_insertTotalCholesterolObjectWithModel:(MJTotalCholesterolModel *)model{
    
    MJTotalCholesterolModel *itemModel = [WCBDHelper.shared.dataBase getObjectOfClass:MJTotalCholesterolModel.class fromTable:kTotalCholesterolTable where:MJTotalCholesterolModel.detectionId == model.detectionId];
    
    if(itemModel){
        BOOL success = [self.dataBase deleteFromTable:kTotalCholesterolTable where:MJTotalCholesterolModel.detectionId == model.detectionId];
         if(success){
             NSLog(@"MJLog=======================总胆固醇删除成功");
         }
    }
    
    BOOL result = [self.dataBase insertObject:model intoTable:kTotalCholesterolTable];
    if(result){
        NSLog(@"MJLog=======================总胆固醇插入成功");
    }
}
+ (void)mj_insertTotalCholesterolObjectsWithArray:(NSArray *)array dataArrayBlock:(WCBDArraySuccessBlock)dataArrayBlock{
    
    NSString *tableName = kTotalCholesterolTable;
    
    if(![WCBDHelper.shared.dataBase tableExists:tableName]){
        BOOL result = [WCBDHelper.shared.dataBase createTable:tableName withClass:MJTotalCholesterolModel.class];
        if (!result) {
            NSLog(@"创建受检者表失败");
        }else{
            NSLog(@"创建受检者表成功");
        }
    }else{
        NSLog(@"存在受检者表");
    }
    
    NSString *userId = UserManager.shared.userModel.userId;
    NSArray *arrayItems =  [NSArray yy_modelArrayWithClass:[MJTotalCholesterolModel class] json:array];
    NSArray* modelArray = [[arrayItems reverseObjectEnumerator] allObjects];

    NSArray<MJTotalCholesterolModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJTotalCholesterolModel.class fromTable:tableName where:MJTotalCholesterolModel.userId == userId];
    
    if(allObjects.count){
        BOOL success = [WCBDHelper.shared.dataBase deleteFromTable:tableName where:MJTotalCholesterolModel.userId == userId];
        if(success){
            BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:tableName];
            if(result){
                NSLog(@"MJLog=======================插入成功");
            }
        }
    }else{
        BOOL result = [WCBDHelper.shared.dataBase insertObjects:modelArray intoTable:tableName];
        if(result){
            NSLog(@"MJLog=======================插入成功");
        }
    }
    if(dataArrayBlock)
     dataArrayBlock(modelArray);
    
}

- (NSArray *)mj_getTotalCholesterolArrayWithParm:(MJCommonModel *)parm {
    
    NSString *userId = UserManager.shared.userModel.userId;
    WCDB::Expression userIdExpression = WCDB::Expression(MJTotalCholesterolModel.userId == userId);

    WCDB::Expression condition = WCDB::Expression(userIdExpression);
    if(parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJTotalCholesterolModel.patientId == parm.patientId);
        condition =  WCDB::Expression(userIdExpression && patientIdExpression);
    }
    if(parm.minDate && parm.maxDate){
        WCDB::Expression dateExpression = WCDB::Expression(MJTotalCholesterolModel.createDateTime > parm.minDate && MJTotalCholesterolModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression);
    }
    if(parm.minDate && parm.maxDate && parm.patientId){
        WCDB::Expression patientIdExpression = WCDB::Expression(MJTotalCholesterolModel.patientId == parm.patientId);
        WCDB::Expression dateExpression = WCDB::Expression(MJTotalCholesterolModel.createDateTime > parm.minDate && MJTotalCholesterolModel.createDateTime < parm.maxDate);
        condition = WCDB::Expression(userIdExpression && dateExpression && patientIdExpression);
    }
    
    NSArray<MJTotalCholesterolModel*>* allObjects = [WCBDHelper.shared.dataBase getObjectsOfClass:MJTotalCholesterolModel.class fromTable:kTotalCholesterolTable where:condition orders:MJTotalCholesterolModel.createDateTime.asOrder(WCTOrderedDescending)];
    return allObjects;
}
@end
